class_name MapConnection
extends Control

var from_node: MapNode
var to_node: MapNode
var connection_line: Line2D
var flow_particles: CPUParticles2D
var flow_tween: Tween
var total_floors: int = 10  # 默认10层

func _init(from: MapNode, to: MapNode):
	from_node = from
	to_node = to
	_setup_connection()

func set_map_info(floors: int):
	total_floors = floors
	# 重新计算连接位置
	_update_connection_points()

func _setup_connection():
	# 创建连线
	connection_line = Line2D.new()
	connection_line.width = 4.0
	connection_line.joint_mode = Line2D.LINE_JOINT_ROUND
	connection_line.end_cap_mode = Line2D.LINE_CAP_ROUND
	add_child(connection_line)
	
	# 创建流动粒子效果
	flow_particles = CPUParticles2D.new()
	flow_particles.emitting = false
	flow_particles.amount = 20
	flow_particles.lifetime = 2.0
	flow_particles.texture = null  # 使用默认圆形粒子
	flow_particles.emission_shape = CPUParticles2D.EMISSION_SHAPE_POINT
	add_child(flow_particles)
	
	# 设置连线点（使用曲线）
	_update_connection_points()
	
	# 更新连接状态
	update_connection()

func _update_connection_points():
	# 计算起点和终点，使用与节点相同的位置计算
	var from_pos = _calculate_node_position(from_node)
	var to_pos = _calculate_node_position(to_node)
	
	print("连接线: ", from_node.get_node_name(), " -> ", to_node.get_node_name())
	print("  起点: ", from_pos, " 终点: ", to_pos)
	
	# 创建贝塞尔曲线控制点
	var mid_y = (from_pos.y + to_pos.y) / 2
	var control_offset = abs(to_pos.x - from_pos.x) * 0.3
	
	var control1 = Vector2(from_pos.x, mid_y + control_offset)
	var control2 = Vector2(to_pos.x, mid_y + control_offset)
	
	# 生成曲线点
	connection_line.clear_points()
	var points = []
	var steps = 20
	
	for i in range(steps + 1):
		var t = float(i) / steps
		var point = _bezier_curve(from_pos, control1, control2, to_pos, t)
		points.append(point)
	
	# 设置连线点
	for point in points:
		connection_line.add_point(point)
	
	# 设置粒子位置和方向
	flow_particles.position = from_pos
	var direction = (to_pos - from_pos).normalized()
	flow_particles.direction = direction
	flow_particles.initial_velocity_min = 50
	flow_particles.initial_velocity_max = 80

func _bezier_curve(p0: Vector2, p1: Vector2, p2: Vector2, p3: Vector2, t: float) -> Vector2:
	# 三次贝塞尔曲线计算
	var u = 1.0 - t
	var tt = t * t
	var uu = u * u
	var uuu = uu * u
	var ttt = tt * t
	
	return uuu * p0 + 3 * uu * t * p1 + 3 * u * tt * p2 + ttt * p3

func _calculate_node_position(node: MapNode) -> Vector2:
	# 计算节点在地图上的实际位置
	# 基于楼层和X位置计算坐标，从下往上排列，每层3个节点
	var floor_spacing = 120.0  # 楼层之间的垂直间距
	var node_spacing = 180.0   # 节点之间的水平间距（增大以适应3节点布局）
	var start_x = 200.0        # 起始X坐标（居中）
	
	# 动态计算地图高度和起始位置（与MapView保持一致）
	var map_height = total_floors * floor_spacing + 200.0  # 动态计算地图高度
	var start_y = map_height - 50.0  # 起始Y坐标（从底部开始，留一些边距）
	
	# 居中显示3个节点
	var x = start_x + node.x_position * node_spacing
	var y = start_y - (node.floor_number - 1) * floor_spacing  # 从下往上，所以用减法
	
	return Vector2(x, y)

func update_connection():
	# 更新连线状态
	if to_node.is_visited:
		# 已走过的路径：蓝色虚线
		connection_line.default_color = Color(0.4, 0.6, 1.0, 0.8)
		connection_line.width = 3.0
		connection_line.texture = null  # 实线
		flow_particles.emitting = false
	elif to_node.is_current:
		# 当前路径：白色发光
		connection_line.default_color = Color(1.0, 1.0, 1.0, 1.0)
		connection_line.width = 5.0
		connection_line.texture = null
		flow_particles.emitting = true
		flow_particles.color = Color(1.0, 1.0, 1.0, 0.9)
	elif to_node.is_accessible:
		# 可选择的路径：黄色实线，有流动效果
		connection_line.default_color = Color(1.0, 1.0, 0.6, 0.9)
		connection_line.width = 4.0
		connection_line.texture = null
		flow_particles.emitting = true
		flow_particles.color = Color(1.0, 1.0, 0.6, 0.8)
	else:
		# 不可访问的路径：灰色虚线
		connection_line.default_color = Color(0.3, 0.3, 0.3, 0.4)
		connection_line.width = 2.0
		connection_line.texture = null
		flow_particles.emitting = false
